import { Dictionary } from "../Common/Dictionary";
import { IADQContaniner, IRegistrationBuilder } from "../Interfaces/IIOC";
import { IDictionary } from "../Interfaces/IUtils";
import { RegistrationBuilder } from "./RegistrationBuilder";
/**
 * ADQ IOC 容器
 */
export class ADQContaniner implements IADQContaniner {
  ResolveList<IT>(ifname: string): IT[] {
    var register = this.dicList.get(ifname);
    if (!register) {
      console.error("resulve error:cannot find interface");
      return null;
    }
    var list = new Array<IT>();
    for (let i = 0; i < register.length; i++) {
      const element = register[i].transferData;
      list.push(element);
    }
    return list;
  }
  RegisterType<IT, T>(): IRegistrationBuilder {
    throw new Error("Method not implemented.");
  }
  ResolveType<IT>() {
    throw new Error("Method not implemented.");
  }
  Register(ifname: string, classType: Function): IRegistrationBuilder {
    var register: IRegistrationBuilder = new RegistrationBuilder(
      ifname,
      classType
    );
    var registerArray = this.dicList.get(ifname);
    if (!registerArray) {
      registerArray = new Array<IRegistrationBuilder>();
    }
    registerArray.unshift(register);

    this.dicList.set(ifname, registerArray);
    return register;
  }
  ResolveToFunc(ifname: string): Function {
    var register = this.dicList.get(ifname);
    if (!register) {
      console.error("resulve error:cannot find interface");
      return null;
    }
    return register[0].classType;
  }
  Resolve<IT>(ifname: string): IT {
    var register = this.dicList.get(ifname);
    if (!register) {
      console.error("resulve error:cannot find interface");
      return null;
    }
    return register[0].transferData;
  }

  /**容器字典集合 */
  dicList: IDictionary<string, Array<IRegistrationBuilder>>;
  constructor() {
    this.dicList = new Dictionary<string, Array<IRegistrationBuilder>>();
  }
}
